package edu.mse.camel.client.whiteboard.gef.ui.figures;


import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.draw2d.text.FlowPage;
import org.eclipse.draw2d.text.ParagraphTextLayout;
import org.eclipse.draw2d.text.TextFlow;
/**
 * An figure that draws an ellipse filling its bounds.
 */
public class EllipseFigure 	extends Shape implements IFigureWithText
{
	private TextFlow textFlow;
/**
 * Constructs a new Ellipse with the default values of a Shape.
 * @since 2.0
 */
public EllipseFigure() {
	setBorder(new MarginBorder(3));
	FlowPage flowPage = new FlowPage();
	textFlow = new TextFlow();

	textFlow.setLayoutManager(new ParagraphTextLayout(textFlow,
					ParagraphTextLayout.WORD_WRAP_TRUNCATE));

	flowPage.add(textFlow);
    flowPage.setHorizontalAligment(PositionConstants.CENTER);
	setLayoutManager(new StackLayout());
	add(flowPage);
	
	
}

/**
 * Returns <code>true</code> if the given point (x,y) is contained within this ellipse.
 * @param x the x coordinate
 * @param y the y coordinate
 * @return <code>true</code>if the given point is contained
 */
public boolean containsPoint(int x, int y) {
	if (!super.containsPoint(x, y))
		return false;
	Rectangle r = getBounds();
	long ux = x - r.x - r.width / 2;
	long uy = y - r.y - r.height / 2;
	return ((ux * ux) << 10) / (r.width * r.width) 
		 + ((uy * uy) << 10) / (r.height * r.height) <= 256;
}

/**
 * Fills the ellipse.
 * @see org.eclipse.draw2d.Shape#fillShape(org.eclipse.draw2d.Graphics)
 */
protected void fillShape(Graphics graphics) {
	graphics.fillOval(getBounds());
}

/**
 * Outlines the ellipse.
 * @see org.eclipse.draw2d.Shape#outlineShape(org.eclipse.draw2d.Graphics)
 */
protected void outlineShape(Graphics graphics) {
	Rectangle r = Rectangle.SINGLETON;
	r.setBounds(getBounds());
	r.width--;
	r.height--;
	r.shrink((lineWidth - 1) / 2, (lineWidth - 1) / 2);
	graphics.drawOval(r);
}
public String getText() {
	return textFlow.getText();
}
public void setText(String newText) {
	textFlow.setText(newText);
}

}
